package com.yj.springbootjava8.stream;

import lombok.Data;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * java8 Stream 流
 */
public class Java8_Stream {
    public static void main(String[] args) {

        // 1.  通过 java.util.Collection.stream() 方法用`集合`来创建流
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.stream();


        // 2.  使用java.util.Arrays.stream(T[]array) 方法用`数组`来创建流
        Integer [] arr = new Integer[]{1,2,3};
        Arrays.stream(arr);


        // 3.  使用Stream 的静态方法：of()、iterate()、generate()
        Stream<Integer> stream = Stream.of(1, 2, 3);

        Stream<Integer> limit = Stream.iterate(0, (x) -> x + 3).limit(4);
        limit.forEach(System.out::println);

        Stream<Double> limit1 = Stream.generate(Math::random).limit(3);
        limit1.forEach(System.out::println);


        List<Integer> integers = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
        Optional<Integer> findFirst = integers.stream().filter(x -> {
            return x > 6;
        }).findFirst();
        System.out.println(findFirst.get());

        Optional<Integer> findAny = integers.stream().filter(x -> {
            return x > 6;
        }).findAny();
        System.out.println(findAny.get());

        boolean anyMatch = integers.stream().anyMatch(x -> {
            return x < 6;
        });
        System.out.println(anyMatch);

        System.out.println("匹配第一个值：" + findFirst.get());
        System.out.println("匹配任意一个值：" + findAny.get());
        System.out.println("是否存在大于6的值：" + anyMatch);

        System.out.println("筛选出集合中大于7的元素");
        List<Integer> collect = integers.stream().filter(x -> {
            return x > 7;
        }).collect(Collectors.toList());
        System.out.println(collect);

        System.out.println("获取String 集合中最长的元素");
        List<String> strList = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
        String s = strList.stream().max(Comparator.comparingInt(String::length)).get();
        System.out.println(s);
        System.out.println("获取Integer集合中的最大值");
        List<Integer> integerList = Arrays.asList(7, 6, 9, 4, 11, 6);
        Integer max = integerList.stream().max(Comparator.comparingInt(Integer::intValue)).get();
        System.out.println(max);
        System.out.println("计算Integer集合中大于6的元素的个数");
        long count = integerList.stream().filter(x -> x > 6).count();
        System.out.println("集合中大于6的元素的个数:"+count);
        System.out.println("英文字符串数组的元素全部改为大写。整数数组每个元素+3");
        String[] strArr = { "abcd", "bcdd", "defde", "fTr" };
        Stream<String> stringStream = Arrays.stream(strArr);
        List<String> stringList = stringStream.map(s1 -> s1.toUpperCase()).collect(Collectors.toList());
        System.out.println(stringList);
        List<Integer> intList = Arrays.asList(1, 3, 5, 7, 9, 11);
        List<Integer> integers1 = intList.stream().map(integer -> integer + 3).collect(Collectors.toList());
        System.out.println(integers1);
        System.out.println("将两个字符数组合并成一个新的字符数组");
        List<String> list2 = Arrays.asList("m-k-l-a", "1-3-5-7");
        System.out.println(list2);
        List<String> collect1 = list2.stream().flatMap(s1 -> {
            String[] split = s1.split("-");
            Stream<String> stream1 = Arrays.stream(split);
            return stream1;
        }).collect(Collectors.toList());
        System.out.println(collect1);
        System.out.println("求Integer集合的元素之和、乘积和最大值");
        List<Integer> list3 = Arrays.asList(1, 3, 2, 8, 11, 4);
        //求和方式1
        Integer integer1 = list3.stream().reduce((integer, integer2) -> {
            return integer + integer2;
        }).get();
        System.out.println(integer1);
        //求和方式2
        integer1 = list3.stream().reduce(Integer::sum).get();
        System.out.println(integer1);
        //求和方式3
        integer1 = list3.stream().reduce(0, Integer::sum);
        System.out.println(integer1);
        //求最大值 写法1
        Integer integer = list3.stream().reduce((x, y) -> {
            return x > y ? x : y;
        }).get();
        System.out.println(integer);
        //求最大值 写法2
        Integer integer2 = list3.stream().reduce(1, Integer::max);
        System.out.println(integer2);

    }
}


